<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\RoomTemplatePull\Action\Sidebar;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Utils\Codec\Json;
use MoChat\App\Common\Middleware\SidebarAuthMiddleware;
use MoChat\App\WorkEmployee\Contract\WorkEmployeeContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Plugin\RoomTemplatePull\Contract\RoomTemplatePullContract;

/**
 * 详情-群模板.
 *
 * Class StoreTemplate
 * @Controller
 */
class Show extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var RoomTemplatePullContract
     */
    protected $roomTemplatePullService;

    /**
     * @Inject
     * @var WorkEmployeeContract
     */
    protected $workEmployee;

    /**
     * @Middlewares({
     *     @Middleware(SidebarAuthMiddleware::class)
     * })
     * @RequestMapping(path="/sidebar/roomTemplatePull/show", methods="get")
     */
    public function handle(): array
    {
        // 接收参数
        $params = [
            'id' => (int) $this->request->input('id'),
        ];
        // 校验参数
        $this->validated($params);

        $info = $this->roomTemplatePullService->getRoomTemplatePullById($params['id'], ['id', 'name', 'employees']);
        $employeeIds = Json::decode($info['employees']);
        $employees = $this->workEmployee->getWorkEmployeesById($employeeIds, ['id', 'name', 'wx_user_id', 'avatar']);
        foreach ($employees as $k => $v) {
            $employees[$k]['avatar'] = file_full_url($v['avatar']);
        }
        $info['employees'] = $employees;
        return $info;
    }

    /**
     * @return string[] 规则
     */
    public function rules(): array
    {
        return [
            'id' => 'required',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    public function messages(): array
    {
        return [
            'id.required' => '拉群模板id 必传',
        ];
    }
}
